package com.mlethe.android.demo.wheelview;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.annotation.Nullable;

import com.mlethe.android.demo.R;
import com.mlethe.android.demo.wheelview.entities.CityEntity;
import com.mlethe.android.demo.wheelview.utils.ParseHelper;
import com.mlethe.library.dialog.DialogFragment;
import com.mlethe.library.widget.wheelview.WheelView;
import com.mlethe.library.widget.wheelview.picker.OptionsPickerView;
import com.mlethe.library.widget.wheelview.picker.listener.OnOptionsSelectedListener;
import com.mlethe.library.widget.wheelview.picker.listener.OnPickerScrollStateChangedListener;

import java.util.ArrayList;
import java.util.List;

public class CityPickerDialog extends DialogFragment {
    private OptionsPickerView<CityEntity> mOptionsPickerView;

    private OnOptionsSelectedListener mListener;

    public CityPickerDialog setOnOptionsSelectedListener(OnOptionsSelectedListener listener) {
        this.mListener = listener;
        return this;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_city_picker;
    }

    @Override
    protected void onBindViewHolder(Bundle savedInstanceState, ViewHolder holder) {
        setCancelableOnTouchOutside(true)
                .formBottom(true)
                .fullWidth()
                .setOnDismissListener(dialog -> {
                    mListener = null;
                    mOptionsPickerView = null;
                });
        mOptionsPickerView = holder.getView(R.id.opv_city);
        mOptionsPickerView.setLineSpacing(15, true);


        final Button cancelBtn = holder.getView(R.id.btn_cancel);
        final Button okBtn = holder.getView(R.id.btn_ok);
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        okBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onOptionsSelected(mOptionsPickerView.getOpt1SelectedPosition(),mOptionsPickerView.getOpt1SelectedData(),
                            mOptionsPickerView.getOpt2SelectedPosition(),mOptionsPickerView.getOpt2SelectedData(),
                            mOptionsPickerView.getOpt3SelectedPosition(),mOptionsPickerView.getOpt3SelectedData());
                }
                dismiss();
            }
        });

        mOptionsPickerView.setOnOptionsSelectedListener(new OnOptionsSelectedListener<CityEntity>() {
            @Override
            public void onOptionsSelected(int opt1Pos, @Nullable CityEntity opt1Data, int opt2Pos,
                                          @Nullable CityEntity opt2Data, int opt3Pos, @Nullable CityEntity opt3Data) {

            }
        });

        mOptionsPickerView.setOnPickerScrollStateChangedListener(new OnPickerScrollStateChangedListener() {
            @Override
            public void onScrollStateChanged(int state) {
                okBtn.setEnabled(state == WheelView.SCROLL_STATE_IDLE);
            }
        });
        List<CityEntity> p3List = new ArrayList<>(1);
        List<List<CityEntity>> c3List = new ArrayList<>(1);
        List<List<List<CityEntity>>> d3List = new ArrayList<>(1);
        ParseHelper.initThreeLevelCityList(getContext(), p3List, c3List, d3List);
        mOptionsPickerView.setLinkageData(p3List, c3List, d3List);
    }
}
